<template>
<div><van-image
  round
  width="10rem"
  height="10rem"
  src="https://img01.yzcdn.cn/vant/cat.jpeg"
/>
<van-sticky :offset-top="280">
    <van-form @submit="onSubmit">
  <van-field
    v-model="username"
    name="用户名"
    label="用户名"
    placeholder="用户名"
    :rules="[{ required: true, message: '请填写用户名' }]"
  />
  <van-field
    v-model="password"
    type="password"
    name="密码"
    label="密码"
    placeholder="密码"
    :rules="[{ required: true, message: '请填写密码' }]"
  />
  <div style="margin: 16px;">
    <van-button round block type="info" native-type="submit" @click="login">登录</van-button>
  </div>
</van-form>
</van-sticky>
</div>
</template>

<script>
    export default {
  data() {
    return {
         username: '',
      password: '',
    };
  },
  methods: {
    onSubmit(values) {
      console.log('submit', values);
    },
    login:function(){
      this.$axios.post('inspectorLogin',{userName:this.username,password:this.password})
      .then(res=>{
        if(res.data.code==200){
        var token = res.data.data.token;
        var sectionid = res.data.data.sections
        var insceptor =res.data.data.rows
        var insceptorid = res.data.data.rows.id
        var nickName =res.data.data.rows.nickName
                      // 前端局部存储
                      localStorage.setItem("Authorization",token);
                      localStorage.setItem("sectionid",sectionid);
                      localStorage.setItem("insceptor",insceptor);
                     localStorage.setItem("insceptorid",insceptorid);
                      localStorage.setItem("nickName",nickName);
                      this.$router.push("/main/");
        }else{
          alert("用户名或密码错误")
          this.$message.error(res.data.msg)
        }
      })
      .error(error=>{
                alert('登录失败')
            });
    }
  },
};
</script>


